<template>
  <transition name="slide">
    <music-list :title="title" :bg-image="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script type="text/ecmascript-6">
import MusicList from 'components/music-list/music-list'
import {getSongList} from 'api/recommend'
import {mapGetters} from 'vuex'
import {createListSong} from 'common/js/song'
export default {
  computed: {
    title() {
      return this.disc.name
    },
    bgImage() {
      return this.disc.coverImgUrl
    },
    ...mapGetters([
      'disc'
    ])
  },
  data() {
    return {
      songs: []
    }
  },
  created() {
    this._getSongList()
  },
  methods: {
    _getSongList() {
      if (!this.disc.id) {
        this.$router.push('/recommend')
        return
      }
      getSongList(this.disc.id).then((res) => {
        if (res.code === 200) {
          this.songs = this._normalizeSongs(res.data.tracks)
        }
      })
    },
    // 重新处理list，将歌曲相关信息存放入song对象中，最后push成json
    _normalizeSongs(list) {
      let ret = []
      list.forEach((item) => {
        if (item.id) {
          ret.push(createListSong(item))
        }
      })
      return ret
    }
  },
  components: {
    MusicList
  }
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .slide-enter-active, .slide-leave-active
    transition: all 0.3s

  .slide-enter, .slide-leave-to
    transform: translate3d(100%, 0, 0)
</style>